<?php

namespace Modules\DataCheck\Services;

use Illuminate\Support\Facades\Cache;

class TaskStopService
{
    private static string $cachePrefix = 'data_check_stop_task:';

    /**
     * 停止任务
     * @param string $taskId
     * @param int    $expireTime 过期时间
     * @return void
     */
    public static function stop(string $taskId, int $expireTime = 600): void
    {
        // 如果不指定过期时间，则默认一年
        $expireTime = $expireTime ?: 31536000;
        Cache::put(self::getCacheKey($taskId), time(), $expireTime);
    }

    /**
     * 判断是否停止
     * @param string $taskId
     * @return bool
     */
    public static function isStop(string $taskId): bool
    {
        return Cache::has(self::getCacheKey($taskId));
    }

    /**
     * 清理缓存
     * @param string $taskId
     * @return void
     */
    public static function clear(string $taskId): void
    {
        Cache::forget(self::getCacheKey($taskId));
    }

    /**
     * 获取缓存key
     * @param string $taskId
     * @return string
     */
    private static function getCacheKey(string $taskId): string
    {
        return self::$cachePrefix . $taskId;
    }
}